/**
 * ajax 请求{not async}
 * 
 * @param {}
 *            url
 * @return {}
 */
function sendRequest(url) {
	var xmlhttp = document.all
			? new ActiveXObject("Msxml2.XMLHTTP")
			: new XMLHttpRequest();
	xmlhttp.open("POST", url, false);
	xmlhttp.send(null);
	return xmlhttp.responseText;
}

/**
 * 获取引用js文件时携带的参数
 * 
 * @param {}
 *            fileName
 * @param {}
 *            key
 * @return {}
 */
function getParameter(fileName, key) {
	// 获取到所有<script>对象
	var val = null;
	var scripts = document.getElementsByTagName("script");
	for (var i = 0; i < scripts.length; i++) {
		var src = scripts[i].src;
		// 取得你想要的js文件名
		if (src.indexOf(fileName) !== -1) {
			// 获取js文件名后面的所有参数
			src = src.substring(src.lastIndexOf(fileName + "?")
					+ (fileName.length + 1));
			var array = src.split("&");
			// 将参数一个一个遍历出来
			for (var j = 0; j < array.length; j++) {
				var finalObj = array[j].split("=");
				// console.log("参数：" + finalObj[0] + "值：" +
				// finalObj[1]);
				if (finalObj[0] == key) {
					val = finalObj[1];
					break;
				}
			}
		}
	}

	return val;
}

/**
 * groove remix data
 * 
 * @param {}
 *            tableNames 数据表名称{table1,table2}
 * @param {}
 *            counterNames 数据计数器{counter1,counter2}
 * @param {}
 *            handleTypes 数据操作方式{single,multi}
 * @param {}
 *            lis 数据加工监听器
 * @param {}
 *            thisForm 表单域对象
 * @param {}
 *            thisFormValidate 表单域有效性校验对象
 * @param {}
 *            thisFormSuccess 提交操作成功处理函数
 * @param {}
 *            thisFormError 提交操作失败处理函数
 */
function grooveRemixSubmit(tableNames, counterNames, handleTypes, lis,
		thisForm, thisFormValidate, thisFormSuccess, thisFormError) {

	$("<input type=\"hidden\" id=\"tableNames\" name=\"tableNames\" value=\""
			+ tableNames + "\">").appendTo(thisForm);
	$("<input type=\"hidden\" id=\"counterNames\" name=\"counterNames\" value=\""
			+ counterNames + "\">").appendTo(thisForm);
	$("<input type=\"hidden\" id=\"handleTypes\" name=\"handleTypes\" value=\""
			+ handleTypes + "\">").appendTo(thisForm);
	if (lis != null) {
		$("<input type=\"hidden\" id=\"listener\" name=\"listener\" value=\""
				+ lis + "\">").appendTo(thisForm);
	}

	var options = {
		beforeSubmit : function(formData, jqForm, options) {
			if (thisFormValidate)
				return thisFormValidate();
		},
		success : function showResponse(responseText, statusText) {
			thisForm[0].reset()

			if (thisFormSuccess) {
				thisFormSuccess(responseText, statusText);
			} else {
				alert("status: " + statusText + "\n\nresponseText: \n"
						+ responseText + "\n\n");
			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			if (thisFormError) {
				thisFormError(XMLHttpRequest, textStatus, errorThrown);
			} else {
				alert("responseText: " + XMLHttpRequest.responseText
						+ ", statusText: " + XMLHttpRequest.statusText);
			}
		},
		url : context + "/system/baseworkremix.do", // override
		// contentType : "application/json",
		type : "post",
		resetForm : true,
		iframe : (thisForm.attr("enctype") == "multipart/form-data")
	};

	thisForm.ajaxSubmit(options);
}

/**
 * groove delete data
 * 
 * @param {}
 *            tableNames 数据表名称{table1,table2}
 * @param {}
 *            counterNames 数据计数器{counter1,counter2}
 * @param {}
 *            handleTypes 数据操作方式{single,multi}
 * @param {}
 *            lis 数据加工监听器
 * @param {}
 *            jsonParam json类型的参数
 * @param {}
 *            thisForm 表单域对象
 * @param {}
 *            thisFormValidate 表单域有效性校验对象
 * @param {}
 *            thisFormSuccess 提交操作成功处理函数
 * @param {}
 *            thisFormError 提交操作失败处理函数
 */
function grooveDeleteSubmit(tableNames, counterNames, handleTypes, lis,
		jsonParam, thisFormValidate, thisFormSuccess, thisFormError) {
	$.ligerDialog.confirm("确定提交删除数据", function(yes) {
		if (yes) {
			jsonParam.tableNames = tableNames;
			jsonParam.counterNames = counterNames;
			jsonParam.handleTypes = handleTypes;
			if (lis != null) {
				jsonParam.lis = lis;
			}

			var str = "";

			$.each(jsonParam, function(index) {
						var val = jsonParam[index];
						str += (index + "=" + val + "&");
					});

			$.ajax({
				url : context + "/system/baseworkdelete.do",
				data : str,
				type : "post",
				beforeSend : function(XMLHttpRequest) {
					if (thisFormValidate) {
						return thisFormValidate();
					}

					return true;
				},
				success : function(data) {
					if (thisFormSuccess != null) {
						thisFormSuccess(data);
					}
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					if (thisFormError) {
						thisFormError(XMLHttpRequest, textStatus, errorThrown);
					} else {
						alert("responseText: " + XMLHttpRequest.responseText
								+ ", statusText: " + XMLHttpRequest.statusText);
					}
				}
			});
		}
	});

}

/**
 * 单表查询
 * 
 * @param {}
 *            params { id 主键值, tableName, orderByName, orderBy }
 */
function grooveTableSelector(params) {
	if (params != null) {
		var paramData = json2param(params);

		$.ajax({
					url : context + '/system/baseworkselect.do',
					processData : false,
					data : paramData,
					success : function(data) {
						var responseData = param2json(data);
						if (responseData.success) {
							if (params.initFunc != null) {
								params.initFunc(responseData.data);
							}
						}
					},
					error : function() {
						alert("baseworkselect服务出现异常");
					}
				});
	} else {
		alert("baseworkselect需要params");
	}
}

/**
 * 单表查询
 * 
 * @param {}
 *            params { id 字段值, tableName, handleType, orderByName, orderBy }
 * 
 */
function grooveTableSelector(params) {
	if (params != null) {
		var paramData = json2param(params);

		$.ajax({
					url : context + '/system/baseworkselect.do',
					processData : false,
					data : paramData,
					success : function(data) {
						var responseData = param2json(data);
						if (responseData.success) {
							if (params.initFunc != null) {
								params.initFunc(responseData.data);
							}
						}
					},
					error : function() {
						alert("baseworkselect服务出现异常");
					}
				});
	} else {
		alert("baseworkselect需要params");
	}
}

/**
 * 单表查询
 * 
 * @param {}
 *            params { id 字段值, columnName 字段名称, tableName, handleType,
 *            orderByName, orderBy }
 * 
 */
function grooveTableSelectorI(params) {
	if (params != null) {
		var paramData = json2param(params);

		$.ajax({
					url : context + '/system/baseworkselectI.do',
					processData : false,
					data : paramData,
					success : function(data) {
						var responseData = param2json(data);
						if (responseData.success) {
							if (params.initFunc != null) {
								params.initFunc(responseData.data);
							}
						}
					},
					error : function() {
						alert("baseworkselect服务出现异常");
					}
				});
	} else {
		alert("baseworkselect需要params");
	}
}

/**
 * 主/子表查询
 * 
 * @param {}
 *            params { id 主键字段, primaryTable, foreignTables 外键表, { foreignTable,
 *            listid 返回数据结果集名称, orderby, sort } }
 * 
 */
function grooveTableSelectorII(params) {
	if (params != null) {
		var paramData = json2param(params);

		$.ajax({
					url : context + '/system/baseworkselectII.do',
					processData : false,
					data : paramData,
					success : function(data) {
						var responseData = param2json(data);
						if (responseData.success) {
							if (params.initFunc != null) {
								params.initFunc(responseData.data);
							}
						}
					},
					error : function() {
						alert("baseworkselect服务出现异常");
					}
				});
	} else {
		alert("baseworkselect需要params");
	}
}

/**
 * 查看附件
 * 
 * @param {}
 *            title 附件名称
 * @param {}
 *            id 附件id
 */
function viewAnnex(title, id) {
	/*
	 * $.ligerDialog.open({ title : title, isResize : true, width : (width ==
	 * null ? 800 : width), height : (height == null ? 500 : height), url :
	 * context + "/system/annex.do?method=seeit&id=" + id });
	 */
	var url = context + "/system/annex.do?method=view&id=" + id;
	if ($("#viewAnnexFrame").length == 0) {
		$("<iframe id=\"#viewAnnexFrame\" style=\"display:none;\" width=\"0\" height=\"0\" src=\""
				+ url + "\"></iframe>").appendTo(document.body);
	} else {
		$("#viewAnnexFrame").attr("src", url);
	}
}

/**
 * 下载附件
 * 
 * @param {}
 *            id 附件id
 */
function downloadAnnex(id) {
	/*
	 * $.ligerDialog.open({ title : title, isResize : true, width : (width ==
	 * null ? 800 : width), height : (height == null ? 500 : height), url :
	 * context + "/system/annex.do?method=seeit&id=" + id });
	 */
	var url = context + "/system/annex.do?method=load&id=" + id;
	if ($("#viewAnnexFrame").length == 0) {
		$("<iframe id=\"#viewAnnexFrame\" style=\"display:none;\" width=\"0\" height=\"0\" src=\""
				+ url + "\"></iframe>").appendTo(document.body);
	} else {
		$("#viewAnnexFrame").attr("src", url);
	}
}

/**
 * 删除附件
 * 
 * @param {}
 *            id 附件id
 * @param {}
 *            func 回调函数
 */
function deleteAnnex(id, func) {
	$.ligerDialog.confirm("确定删除附件", function(yes) {
		if (yes) {
			$.ajax({
						url : context + "/system/annex.do?method=delete",
						data : {
							id : id
						},
						type : "post",
						beforeSend : function(XMLHttpRequest) {
							return true;
						},
						success : function(data) {
							var responseData = param2json(data);
							if (responseData.success) {
								if (func && typeof func == 'function') {
									func(responseData);
								}
							} else {
								alert("删除附件出现异常[" + responseData.errCode + "]");
							}
						}
					});
		}
	})
}